'use strict';

angular.module('app')
	.controller('platSubscribeCountController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '平台订阅数统计';
        $scope.param = { };
        $scope.loading = false;
        var myChart = echarts.init(document.getElementById('main'));
		var option="";
		$scope.search = function () {
            $scope.param.type= '1';
            $scope.param.sourceType= '';
	        $scope.loading = false;

			$.ajax({
				url : $scope.app.project_name + '/platSubscribe/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
                    myChart.clear();
                    myChart.showLoading();
                    $scope.setoption(result.data);
				} else {
                    myChart.hideLoading();
					$scope.msg = result.msg;

				}

				$scope.$apply();
			});
		}
		$scope.setoption=function(data){
            var timeArr=[];
            var titleArr=[];
            var seriesArr=new Array();
            var countList=data[0].countList;
            for(var j=0;j<countList.length;j++){
                timeArr[j]=countList[j].countTime;
            }
            var label='{"normal":{"show":"true","position":"top"}}';
            for(var i=0;i<data.length;i++){
                var channelMap=data[i];
                var countList=data[i].countList;
                var dataArr=new Array();
                for(var j=0;j<countList.length;j++){
                    dataArr[j]=countList[j].countNum;
                }
                var channelName=channelMap.channelName;
                titleArr[i]=channelName;
                var seriesStr='{"name":"'+channelName+'","type":"line","data":['+dataArr+'],"label":'+label+'}';
                seriesArr[i]=JSON.parse(seriesStr);
            }
                                            option = {
                                                title: {
                                                    text: ''
                                                },
                                                tooltip: {
                                                    trigger: 'axis'
                                                },
                                                legend: {
                                                    data:titleArr
                                                },
                                                grid: {
                                                    left: '3%',
                                                    right: '4%',
                                                    bottom: '3%',
                                                    containLabel: true
                                                },
                                                toolbox: {

                                                },
                                                xAxis: {
                                                    type: 'category',
                                                    boundaryGap: false,
                                                    data: timeArr
                                                },
                                                yAxis: {
                                                    type: 'value'
                                                },
                                                series:seriesArr
                                            };
                                            myChart.setOption(option);
                                             myChart.hideLoading();
										}
        $scope.search();

		$scope.searchRe=function(){
            $scope.param.startTime=$("#startTime").val();
            $scope.param.endTime=$("#endTime").val();
            if($scope.param.endTime!="" && $scope.param.startTime==""){
                $scope.alertModal({'content': '请选择开始时间'});
                return;
            }

            $.ajax({
                url : $scope.app.project_name + '/platSubscribe/list',
                data: $scope.param
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    myChart.clear();
                    myChart.showLoading();
                    $scope.setoption(result.data);
                } else {
                    $scope.msg = result.msg;
                    myChart.hideLoading();
                }
                $scope.$apply();
            });
        }


                                        $scope.getchannelList = function () {
                                            $.ajax({
                                                url : $scope.app.project_name + '/platSubscribe/platselectlist'
                                            }).then(function(result) {
                                                if (result.httpCode == 200) {
                                                    $scope.channelList= result.data;
                                                    console.log(result.data);
                                                } else {
                                                    $scope.msg = result.msg;
                                                }
                                                $scope.$apply();
                                            });
                                        }
                                        $scope.getchannelList();
                                        $scope.clearSearch = function() {
                                            $("#startTime").val("");
                                            $("#endTime").val("");
                                            $scope.search();
                                        }
} ]);